<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015-07-21
 * Time: 16:03
 */
/**
 * 我的消息
*/
class mobileapi_rpc_mymessage extends mobileapi_frontpage{
    function __construct($app){
        parent::__construct($app);
        $this->verify_member();
    }

    /**
     * 返回未读评论数，点赞数，粉丝数,系统消息接口
    */
    function mymsg(){
//        获取当前登录的用户id
        $uid = $this->get_current_member();
        $uid = $uid['member_id'];
//        获取当前用户口碑的评论未读总数和最新的前3条记录
        $f1 = array(
            'member_id'=>$uid,
            'message_type'=>'comment',
            'is_read'=>'0'
        );
        $comment['count'] = app::get('b2c')->model('member_message')->count($f1);
        if($comment['count'] > 0){
            if($comment['count'] <= 3 ){
                $comment['res'] = app::get('b2c')->model('goods_opinions')->get_comment_list($uid,$comment['count']);
            }else{
                $comment['res'] = app::get('b2c')->model('goods_opinions')->get_comment_list($uid,3);
            }
        }else{
            $comment['res'] = array();
        }
//      获取当前用户的点赞数和点赞用户信息
        $f2 = array(
            'member_id'=>$uid,
            'message_type'=>'praise',
            'is_read'=>'0'
        );
        $praise['count'] = app::get('b2c')->model('member_message')->count($f2);
        if($praise['count'] > 0){
            if($praise['count'] <= 5){
                $praise['res'] = app::get('b2c')->model('goods_opinions')->get_praise_list($uid,$praise['count']);
            }else{
                $praise['res'] = app::get('b2c')->model('goods_opinions')->get_praise_list($uid,5);
            }
        }else{
            $praise['res'] = array();
        }

//      获取当前用户的粉丝数和粉丝用户信息
        $f3 = array(
            'member_id'=>$uid,
            'message_type'=>'fans',
            'is_read'=>'0'
        );
        $fans['count'] = app::get('b2c')->model('member_message')->count($f3);
        if($fans['count'] > 0){
            if($fans['count'] <= 5){
                $fans['res'] = app::get('b2c')->model('member_fans')->get_fans_list($uid,$fans['count']);
            }else{
                $fans['res'] = app::get('b2c')->model('member_fans')->get_fans_list($uid,5);
            }
        }else{
            $fans['res'] = array();
        }
//      获取当前用户的系统消息
        $f4 = array(
            'member_id'=>$uid,
            'message_type'=>'sysmsg',
            'is_read'=>'0'
        );
        $sysmsg['count'] = app::get('b2c')->model('member_message')->count($f4);
        $sysmsg['res'] = app::get('b2c')->model('member_message')->getlist('*',$f4,0,3,'time desc');
        $sysmsg['res'] = $sysmsg['res'] ? $sysmsg['res'] : array();
//      组合成一个数组返回
        $data['comment'] = $comment;
        $data['praise'] = $praise;
        $data['fans'] = $fans;
        $data['sysmsg'] = $sysmsg;

        return  $data;
    }
    /**
     * 返回所有已读的评论数，点赞数，粉丝数，系统消息接口
    */
    function read(){
        $uid = $this->get_current_member();
        $uid = $uid['member_id'];
        $type = $_POST['type'];
        $obj = app::get('b2c')->model('member_message');
        switch($type){
            case 'comment':
                $filter = array(
                    'member_id' => $uid,
                    'message_type'  =>  $type,
                    'is_read'   => 1
                );
                $res = $obj->count($filter);
                break;
            case 'praise':
                $filter = array(
                    'member_id' => $uid,
                    'message_type'  =>  $type,
                    'is_read'   => 1
                );
                $res = $obj->count($filter);
                break;
            case 'fans':
                $filter = array(
                    'member_id' => $uid,
                    'message_type'  =>  $type,
                    'is_read'   => 1
                );
                $res = $obj->count($filter);
            break;
            case 'sysmsg':
                $filter = array(
                    'member_id' => $uid,
                    'message_type'  =>  $type,
                    'is_read'   => 1
                );
                $res = $obj->count($filter);
            break;
        }
        return $res;
    }
//    返回对应用户所有的系统消息并分页，并将未读系统消息改为已读
    /*public function mysysmsg(){
//      接收传入的偏移量，用作分页
        $offset = $_POST['offset'];
//        获取当前用户id
        $uid = $this->get_current_member();
        $uid = $uid['member_id'];
//        指定消息类型
        $type = 'sysmsg';
//        设置过滤条件，得到所有未读消息
        $filter = array('member_id' => $uid,'message_type' => $type,'is_read' => '0');
        $obj = app::get('b2c')->model('member_message');
        $res = $obj->getList('*',$filter);
//        判断是否有未读消息，有的话就将已读改为未读，没有就直接返回所有该用户的系统消息，并按时间倒序，分页
        if(!empty($res)){
            $res = $obj->getList('*',$filter,$offset,2,'time asc');
            $id = $obj->getList('message_id',$filter);
//          $str = '';
            foreach ($id as $v) {
                $str[] = $v['message_id'];
            }
//          $str = trim($str,',');
//          $filters = array('filter_sql'=> "message_id in ({$str})");
            $filters = array('message_id' => $str);
            $data = array('is_read' => '1');
            $obj->update($data,$filters);
            return $res;
        }else{
            return $res = $obj->getList('*',array('member_id' => $uid,'message_type' => $type),$offset,2,'time asc');
        }
    }*/
//  返回所有系统消息并分页
    public function mysysmsg (){
        $offset = empty($_POST['offset']) ? 0 : $_POST['offset'];
        $limit = empty($_POST['limit']) ? 5 : $_POST['limit'];
        $uid = $this->get_current_member();
        $uid = $uid['member_id'];
        $type = 'sysmsg';
        $obj = app::get('b2c')->model('member_message');
        $res = $obj->getList('*',array('member_id' => $uid,'message_type' => $type),$offset,$limit,'time desc');
        $obj->update(array('is_read'=>1),array('member_id'=>$uid,'message_type'=>$type));
        return $res;
    }
//设置未读的系统消息为已读
    public function toread(){
//        可传入单个消息id或消息id数组
        $msgid = $_POST['message_id'];
//        判断是否传入消息id
        if(!empty($msgid)){
            $uid = $this->get_current_member();
            $uid = $uid['member_id'];
            $type = 'sysmsg';
            $obj = app::get('b2c')->model('member_message');
            $filter = array('message_id' => $msgid,'member_id' => $uid,'message_type' =>$type);
            $data = array('is_read' => '1');
            $res = $obj->update($data,$filter);
//            判断sql更新执行结果,如果成功返回影响数,失败反之
            if(is_numeric($res)){
                return $res;
            }else{
                return '所传消息id错误/该消息已是已读';
            }
        }else{
            return '请传入消息id';
        }
    }
}